﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;
using Microsoft.AspNetCore.Mvc.Routing;

namespace RbacSystem.Controllers
{
    [Route("abc")]
    [ApiController]
    public class TestController : ControllerBase
    {
        public TestController()
        {

        }

        [HttpGet("test/query")]
        public IActionResult Query()
        {
            return Ok();
        }

        [HttpGet]
        public IActionResult GetClassName()
        {
            var list = Assembly.Load("RbacSystem").GetTypes().Where(m => m.BaseType == typeof(ControllerBase)).ToList();

            foreach (var type in list)
            {
                var methods = type.GetMethods().Where(x => x.GetCustomAttributes<HttpMethodAttribute>().Any());
                foreach (var method in methods)
                {
                    var attr = method.GetCustomAttribute<HttpMethodAttribute>();

                }
                
            }

            return Ok();
        }

        [NonAction]
        public List<Type> FindAllDerivedTypes<T>()
        {
            return FindAllDerivedTypes<T>(Assembly.GetAssembly(typeof(T)));
        }

        [NonAction]
        public List<Type> FindAllDerivedTypes<T>(Assembly assembly)
        {
            var derivedType = typeof(T);
            return assembly
                .GetTypes()
                .Where(t =>
                    t != derivedType &&
                    derivedType.IsAssignableFrom(t)
                    ).ToList();

        }
    }
}
